<template>
	<view class="order">
		<view class="priceBox">
			<view class="price bold">￥<text>{{money}}</text></view>
			<view class="font12 color999" v-if="type==2">查看最佳答案</view>
			<view class="font12 color999" v-if="type==3">提问赏金</view>
			<view class="font12 color999" v-if="type==4">加入活动</view>
			<view class="font12 color999" v-if="type==5">购买会员</view>
			<view class="font12 color999" v-if="type==6">发布找人</view>
			<view class="font12 color999" v-if="type==7">购买择校报告</view>
		</view>
		<view class="" v-if="showPay">
			<view class="font14 color999">支付方式</view>
			<view class="payWay">
				<view class="flex alignCenter" v-for="(item,index) in payArr" :key="index" @click="choosePay(index)">
					<image :src="item.icon" mode="widthFix"></image>
					<view class="chooseBox flex alignCenter">
						<view class="payName font16">{{item.title}}</view>
						<label class="radio">
							<radio class="radiuBox" :value="payVal" :checked="payVal==index+1" />
						</label>
					</view>
				</view>
			</view>
		</view>
		<view class="fixedBottomBtn" @click="sure">确认交易</view>

		<!-- 发布找人成功弹框 -->
		<u-popup v-model="sharePopup" type="center" :mask-close-able="false">
			<view class="popup-box">
				<view class="box1" v-if="popupType==1">
					<image src="https://image.alhelp.net/uploads/20231104/8a247b557e81f68bfaf1bd7fcb555e9f.png" mode="widthFix">
					</image>
					<view class="title font16 color333 bold">发布成功</view>
					<view class="tips font14 color000">发朋友圈请他们帮你接力找人吧！</view>
					<view class="btn font14 color000 flex alignCenter" @click="handleSend">发送</view>
					<uni-icons class="clear-icon" type="clear" color="#fff" size="50" @click="handleHidePopup(1)"></uni-icons>
				</view>
				<view class="box2" v-else>
					<view class="tips font14">世界上任意两个素不相识的 <view>人之间</view>
						<view>最最多只需<span>6</span>步！</view>
					</view>
					<image src="./static/u57.jpg" mode="aspectFill"></image>
					<view class="tips font14">获得更多的人脉连接！<view>只需跨出第一步！</view>
					</view>
					<button class="btn font14 flex alignCenter color333" type="primary" @click="handleBook">允许读取通讯录</button>
					<!-- <view class="btn font14 flex alignCenter color333" @click="handleBook">允许读取通讯录</view> -->
					<view class="tips font14">人脉数量增加21倍！</view>
					<uni-icons class="clear-icon" type="clear" color="#fff" size="50" @click="handleHidePopup(2)"></uni-icons>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getOrderAndPay
	} from "@/utils/common.js"
	import {
		entrustFoundPerson
	} from "@/api/lookFor.js"
	import {
		getPDFList
	} from '@/api/toolbox.js'
	export default {
		data() {
			return {
				sharePopup:false,
				payVal: '1',
				payArr: [{
					title: '微信支付',
					icon: '/static/image/icons/wechat.png'
				}, ],
				money: 0,
				// 支付的由头  2查看最佳答案；3赏金；4、活动，5、查看专业详情支付，6、发布找人，7、购买择校报告 8、调剂信息单条支付 9、调剂信息全部支付
				type: 2,
				// 查看答案付费就是答案详情id
				nextId: '',
				// 回答详情需要传入提问标题
				title: '',
				optionQuery: '',
				showPay: true,
				popupType: 1, //1、发布成功弹窗，2、读取通讯录弹窗 
				findUserItem: {}, //委托找人学长学姐数据
				school: {},
				adJustParam: {}, //调剂支付传参信息
			};
		},
		onLoad(option) {
			this.optionQuery = option.query ? JSON.parse(option.query) : ''
			this.money = this.optionQuery ? this.optionQuery.money : 0
			this.type = this.optionQuery ? this.optionQuery.type : 2
			this.nextId = this.optionQuery ? this.optionQuery.id : ''
			this.title = this.optionQuery ? this.optionQuery.quesTitle : '';
			if (this.optionQuery && this.optionQuery.info) {
				this.findUserItem = this.optionQuery ? this.optionQuery.info : {};
			};
			if (this.optionQuery && this.optionQuery.params) {
				this.adJustParam = this.optionQuery ? this.optionQuery.params : {};
			}
			console.log('option', this.optionQuery)
			// ios苹果内支付
			// #ifdef APP-PLUS
			if (uni.getStorageSync('systemInfo').platform == 'ios') {
				this.showPay = false
			}
			// #endif
			this.school = uni.getStorageSync('school')
		},
		methods: {
			async getPDF(id) {
				const data = {}
				data.schoolName = this.school.schoolName
				data.collegeName = this.school.collegeName
				data.majorName = this.school.majorName
				data.schoolCode = this.school.schoolCode
				data.year = this.school.year
				data.collegeCode = this.school.collegeCode
				data.majorCode = this.school.majorCode
				data.userId = uni.getStorageSync('userId')
				data.orderId = id
				data.reportId = 0
				data.price = 0
				data.page = 1
				data.pageSize = 10
				data.type = 1
				const res = await getPDFList(data)
				this.isShow = 1
				uni.redirectTo({
					url: `/pages/toolbox/chooseSchool/download?id=${this.nextId}`
				})

			},
			// 选择支付方式
			choosePay(idx) {
				this.payVal = (Number(idx) + 1).toString()
			},
			sure() {
				if (this.showPay) {
					// 非app ios端走的支付流程
					uni.showLoading({
						title: '提交中',
						mask: true
					})
					let param = {
						id: this.nextId,
						orderType: this.type, //订单类型，是查看答案还是赏金还是其他
						payWay: this.payVal, //1微信；2支付宝；3第三方
						groupId: this.optionQuery.groupId //加入活动的支付要
					}
					// 调剂信息单条支付增加传参
					if (this.type == 8) {
						param.schoolCode = this.adJustParam.schoolCode;
						param.collegeCode = this.adJustParam.collegeCode;
						param.majorCode = this.adJustParam.majorCode
					}
					// #ifdef MP
					param.payProduct = 1 //1小程序；2App
					// #endif
					// #ifdef APP-PLUS
					param.payProduct = 2
					// #endif
					getOrderAndPay(param).then((res) => {
						console.log(res, 'res')
						uni.hideLoading()
						let jumpUrl = ''
						if (this.type == 2) {
							// 支付成功后跳转最佳回答的回答详情查看回答内容
							jumpUrl = `/pages/send/answer/answerDetail/answerDetail?id=${this.nextId}&quesTitle=${this.title}`
							uni.redirectTo({
								url: jumpUrl
							})
						} else if (this.type == 3) {
							// 最佳回答，提问赏金
							uni.navigateBack({
								delta: 1
							})
						} else if (this.type == 4) {
							if (this.optionQuery.fromPage == 'activityDetail') {
								uni.navigateBack({
									delta: 1
								})
							} else {
								uni.redirectTo({
									url: `/pages/send/activity/activityDetail/activityDetail?id=${this.nextId}&groupId=${this.optionQuery.groupId}`
								})
							}
						} else if (this.type == 5) {
							uni.navigateBack();
						} else if (this.type == 6) {
							// 找人支付成功后,弹窗提示
							this.popupType = 1;
							// 判断是否委托,是则调用委托接口，委托成功后跳转找人详情页
							if (this.findUserItem.createUserId) {
								this.findUserItem.ids = [this.nextId];
								console.log(this.findUserItem);
								entrustFoundPerson(this.findUserItem).then(res => {
									if (res.code == 100000) {
										uni.showToast({
											title: '委托成功',
											icon: 'none'
										})
										uni.redirectTo({
											url: `/pages/send/lookFor/lookHomepage/lookHomepage?id=${this.nextId}`
										})
									}
								})
							} else {
								this.sharePopup = true;
							}
						} else if (this.type == 7) {
							this.getPDF(res.data.payOrderId)
						} else {
							uni.navigateBack(1);
						}
					}).catch((err) => {
						uni.hideLoading()
					})
				} else {
					// ios 苹果应用内支付
					uni.getProvider({
						service: 'payment',
						success: (res) => {
							console.error('应用内支付', res)
							const iapChannel = res.providers.find((channel) => {
								return (channel.id === 'appleiap')
							})

							// 如果 iapChannel 为 null，说明当前包没有包含iap支付模块。注意：HBuilder基座不包含 iap 通道
						}
					});
				}
			},

			// 发送
			handleSend() {
				let query = {
					id: this.nextId,
					inviteType: 6
				}
				uni.redirectTo({
					url: '/pages/school/share/share?query=' + JSON.stringify(query)
				})
			},
			// 允许读取通讯录
			handleBook() {
				var _this = this;
				// 获取通讯录对象  
				plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function(addressbook) {
					uni.showToast({
						title: '获取通讯录对象成功',
						duration: 2000
					})
					console.log('获取通讯录对象成功', addressbook)
					// 查找联系人  
					addressbook.find(["displayName", "phoneNumbers"], function(contacts) {
						uni.showToast({
							title: '获取联系人成功',
							duration: 2000
						})
						console.log('获取联系人成功', JSON.stringify(contacts))
						// _this.contactsList = contacts;
					}, function() {
						uni.showToast({
							title: '获取联系人失败',
							duration: 2000
						})
					}, {
						multiple: true
					});
				}, function(e) {
					uni.showToast({
						title: '获取通讯录对象失败:' + e.message,
						duration: 2000
					})
				});
			},
			handleHidePopup(type) {
				// #ifdef APP-PLUS
				this.popupType = 2;
				if (type == 2) {
					this.sharePopup = false;
					// 关闭弹窗,跳转至找人详情页
					uni.redirectTo({
						url: `/pages/send/lookFor/lookHomepage/lookHomepage?id=${this.nextId}`
					})
					return
				}
				// #endif
				this.sharePopup = false;
				uni.redirectTo({
					url: `/pages/send/lookFor/lookHomepage/lookHomepage?id=${this.nextId}`
				})
				return

			},
		}
	}
</script>
<style lang="scss">
	page {
		background: $pageBgColor;
	}
</style>
<style lang="scss" scoped>
	::v-deep .radiuBox .uni-radio-input-checked {
		background-color: $colorMain !important;
		border-color: $colorMain !important;
	}

	::v-deep .radiuBox .uni-radio-input {
		transform: scale(0.7);
	}

	.order {
		padding: 100rpx 30rpx 0;
		box-sizing: border-box;
	}

	.priceBox {
		text-align: center;
		margin: 0 0 100rpx;

		.price {
			text {
				font-size: 70rpx;
			}

		}
	}

	.payWay {
		background: #fff;
		padding: 0 30rpx;
		box-sizing: border-box;
		border-radius: 20rpx;
		margin: 30rpx 0 0;

		&>view:last-child {
			.chooseBox {
				border: none;
			}
		}

		.payName {
			flex: 1;
		}

		image {
			width: 60rpx;
			height: 60rpx;
			margin-right: 20rpx;
			flex-shrink: 0;
		}

		.chooseBox {
			flex: 1;
			padding: 50rpx 0;
			border-bottom: 1px solid $bordercolor;
		}
	}

	.popup-box {
		.box1 {
			width: 540rpx;
			height: 548rpx;
			background: #fff;
			border-radius: 28rpx;
			text-align: center;
			margin: 0 auto;
			line-height: 1em;
			position: relative;

			image {
				width: 152rpx;
				height: 146rpx;
				margin-top: 24rpx;
			}

			.title {
				margin-top: 12rpx;
			}

			.tips {
				line-height: 40rpx;
				margin-top: 40rpx;
				margin-bottom: 80rpx;
			}

			.btn {
				width: 310rpx;
				height: 80rpx;
				background: $colorMain;
				border-radius: 80rpx;
				justify-content: center;
				margin: auto;
			}

			.clear-icon {
				position: absolute;
				margin-top: 160rpx;
				margin-left: -46rpx;
			}
		}

		.box2 {
			width: 480rpx;
			height: 624rpx;
			background: #fff;
			border-radius: 28rpx;
			text-align: center;
			margin: 0 auto;
			line-height: 1em;
			position: relative;

			image {
				width: 256rpx;
				height: 132rpx;
				margin: 30rpx 0 10rpx;
			}

			.tips {
				color: #1E1E1E;
				line-height: 40rpx;
			}

			.tips:first-child {
				padding-top: 40rpx;
			}

			.btn {
				width: 310rpx;
				height: 70rpx;
				background: #fff;
				border-radius: 80rpx;
				justify-content: center;
				border: 1px solid rgba(247, 221, 94, 1);
				margin: 44rpx auto 20rpx;
			}

			.clear-icon {
				position: absolute;
				margin-top: 92rpx;
				margin-left: -46rpx;
			}
		}

	}
</style>